package com.zhouchb.test.反射;

import javax.print.attribute.standard.MediaSize;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @Create 2023-08-27 13:06
 * @Description
 */
public class Person {
    private String name;
    public int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    private void hah() {
        System.out.println("希望能够快速找到工作，保佑保佑");
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class ReflectionTest {
    public static void main(String[] args) throws Exception {
        Class clazz = Person.class;
        Person p = (Person) clazz.newInstance();
        Field age = clazz.getDeclaredField("age");
        age.set(p,10);
        Method getAge = clazz.getDeclaredMethod("getAge");
        getAge.invoke(p);
        System.out.println(p);

        //私有
        Field name = clazz.getDeclaredField("name");
        name.setAccessible(true);
        name.set(p,"林动");

        Method hah = clazz.getDeclaredMethod("hah");
        hah.setAccessible(true);
        System.out.println(p);

        hah.invoke(p);

        //**Class 对象
        Class clazz1 = Person.class;
        Person person = new Person();
        Class clazz2 = person.getClass();
        System.out.println(clazz2);
        Class clazz3 = Class.forName("com.zhouchb.test.反射.Person");
    }
}